home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH5 / SRC / TWEENSMO.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-02  |  26.0 KB  |  832 lines

  1. VERSION 4.00
  2. Begin VB.Form TweenForm 
  3.    Caption         =   "TweenSmooth"
  4.    ClientHeight    =   4590
  5.    ClientLeft      =   2040
  6.    ClientTop       =   1035
  7.    ClientWidth     =   4635
  8.    Height          =   5280
  9.    Left            =   1980
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   306
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   309
  14.    Top             =   405
  15.    Width           =   4755
  16.    Begin VB.CommandButton CmdTween 
  17.       Caption         =   "Tween"
  18.       Height          =   495
  19.       Left            =   3480
  20.       TabIndex        =   12
  21.       Top             =   480
  22.       Width           =   975
  23.    End
  24.    Begin VB.TextBox TweensText 
  25.       Height          =   285
  26.       Left            =   4200
  27.       TabIndex        =   10
  28.       Text            =   "4"
  29.       Top             =   0
  30.       Width           =   375
  31.    End
  32.    Begin VB.TextBox FPSText 
  33.       Height          =   285
  34.       Left            =   4080
  35.       TabIndex        =   9
  36.       Text            =   "20"
  37.       Top             =   1800
  38.       Width           =   375
  39.    End
  40.    Begin VB.CommandButton CmdPlay 
  41.       Caption         =   "Play"
  42.       Default         =   -1  'True
  43.       Height          =   495
  44.       Left            =   3480
  45.       TabIndex        =   7
  46.       Top             =   3480
  47.       Width           =   975
  48.    End
  49.    Begin VB.OptionButton PlayOption 
  50.       Caption         =   "Reversing"
  51.       Height          =   255
  52.       Index           =   2
  53.       Left            =   3360
  54.       TabIndex        =   4
  55.       Top             =   3000
  56.       Width           =   1095
  57.    End
  58.    Begin VB.OptionButton PlayOption 
  59.       Caption         =   "Looping"
  60.       Height          =   255
  61.       Index           =   1
  62.       Left            =   3360
  63.       TabIndex        =   3
  64.       Top             =   2640
  65.       Width           =   1095
  66.    End
  67.    Begin VB.OptionButton PlayOption 
  68.       Caption         =   "Once"
  69.       Height          =   255
  70.       Index           =   0
  71.       Left            =   3360
  72.       TabIndex        =   2
  73.       Top             =   2280
  74.       Value           =   -1  'True
  75.       Width           =   1095
  76.    End
  77.    Begin VB.HScrollBar SBar 
  78.       Height          =   255
  79.       Left            =   0
  80.       Max             =   1
  81.       Min             =   1
  82.       TabIndex        =   1
  83.       Top             =   3960
  84.       Value           =   1
  85.       Width           =   3255
  86.    End
  87.    Begin VB.PictureBox Canvas 
  88.       AutoRedraw      =   -1  'True
  89.       Height          =   3975
  90.       Left            =   0
  91.       ScaleHeight     =   261
  92.       ScaleMode       =   3  'Pixel
  93.       ScaleWidth      =   213
  94.       TabIndex        =   0
  95.       Top             =   0
  96.       Width           =   3255
  97.    End
  98.    Begin VB.Label Label1 
  99.       Caption         =   "Tweens:"
  100.       Height          =   255
  101.       Index           =   2
  102.       Left            =   3360
  103.       TabIndex        =   11
  104.       Top             =   0
  105.       Width           =   615
  106.    End
  107.    Begin VB.Label Label1 
  108.       Caption         =   "FPS:"
  109.       Height          =   255
  110.       Index           =   1
  111.       Left            =   3480
  112.       TabIndex        =   8
  113.       Top             =   1800
  114.       Width           =   375
  115.    End
  116.    Begin MSComDlg.CommonDialog FileDialog 
  117.       Left            =   2640
  118.       Top             =   4200
  119.       _version        =   65536
  120.       _extentx        =   847
  121.       _extenty        =   847
  122.       _stockprops     =   0
  123.       cancelerror     =   -1  'True
  124.    End
  125.    Begin VB.Label FrameLabel 
  126.       Alignment       =   2  'Center
  127.       BorderStyle     =   1  'Fixed Single
  128.       Caption         =   "1/1"
  129.       Height          =   255
  130.       Left            =   1680
  131.       TabIndex        =   6
  132.       Top             =   4320
  133.       Width           =   735
  134.    End
  135.    Begin VB.Label Label1 
  136.       Caption         =   "Frame:"
  137.       Height          =   255
  138.       Index           =   0
  139.       Left            =   1080
  140.       TabIndex        =   5
  141.       Top             =   4320
  142.       Width           =   495
  143.    End
  144.    Begin VB.Menu mnuFile 
  145.       Caption         =   "&File"
  146.       Begin VB.Menu mnuFileLoad 
  147.          Caption         =   "&Load..."
  148.          Shortcut        =   ^L
  149.       End
  150.       Begin VB.Menu mnuFileSave 
  151.          Caption         =   "&Save"
  152.          Shortcut        =   ^S
  153.       End
  154.       Begin VB.Menu mnuFileSaveAs 
  155.          Caption         =   "Save &As..."
  156.          Shortcut        =   ^A
  157.       End
  158.       Begin VB.Menu mnuFileSep1 
  159.          Caption         =   "-"
  160.       End
  161.       Begin VB.Menu mnuFileNew 
  162.          Caption         =   "&New"
  163.          Shortcut        =   ^N
  164.       End
  165.       Begin VB.Menu mnuFileSep2 
  166.          Caption         =   "-"
  167.       End
  168.       Begin VB.Menu mnuFileExit 
  169.          Caption         =   "E&xit"
  170.       End
  171.    End
  172.    Begin VB.Menu mnuFrame 
  173.       Caption         =   "Frame"
  174.       Begin VB.Menu mnuFrameAfter 
  175.          Caption         =   "Insert &After"
  176.       End
  177.       Begin VB.Menu mnuFrameBefore 
  178.          Caption         =   "Insert &Before"
  179.       End
  180.       Begin VB.Menu mnuFrameSep 
  181.          Caption         =   "-"
  182.       End
  183.       Begin VB.Menu mnuFrameClear 
  184.          Caption         =   "&Clear"
  185.       End
  186.       Begin VB.Menu mnuFrameDelete 
  187.          Caption         =   "&Delete"
  188.          Enabled         =   0   'False
  189.       End
  190.    End
  191. Attribute VB_Name = "TweenForm"
  192. Attribute VB_Creatable = False
  193. Attribute VB_Exposed = False
  194. Option Explicit
  195. Dim NumFrames As Integer
  196. Dim Frames() As PolylineFrame
  197. Dim FileLoaded As String
  198. Dim DataModified As Boolean
  199. Dim Playing As Boolean
  200. Dim SelectedFrame As Integer
  201. Dim SelectingFrame As Boolean
  202. Dim Drawing As Boolean
  203. Dim StartX As Integer
  204. Dim StartY As Integer
  205. Dim LastX As Integer
  206. Dim LastY As Integer
  207. ' ************************************************
  208. ' Create the tweens between two key frames using
  209. ' Hermite curves.
  210. ' ************************************************
  211. Sub MakeTweens(key2 As Integer, key3 As Integer)
  212. Dim tween As Integer
  213. Dim pline As Integer
  214. Dim point As Integer
  215. Dim key1 As Integer
  216. Dim key4 As Integer
  217. Dim x1 As Integer
  218. Dim y1 As Integer
  219. Dim x2 As Integer
  220. Dim y2 As Integer
  221. Dim x3 As Integer
  222. Dim y3 As Integer
  223. Dim x4 As Integer
  224. Dim y4 As Integer
  225. Dim dx1 As Integer
  226. Dim dy1 As Integer
  227. Dim dx2 As Integer
  228. Dim dy2 As Integer
  229. Dim t As Single
  230. Dim t2 As Single
  231. Dim t3 As Single
  232. Dim A As Single
  233. Dim B As Single
  234. Dim C As Single
  235. Dim D As Single
  236.     ' Make room for the points.
  237.     For tween = key2 + 1 To key3 - 1
  238.         Frames(tween).NumPolylines = Frames(key2).NumPolylines
  239.         ReDim Frames(tween).Poly(1 To Frames(tween).NumPolylines)
  240.         For pline = 1 To Frames(tween).NumPolylines
  241.             With Frames(tween).Poly(pline)
  242.                 .NumPoints = Frames(key2).Poly(pline).NumPoints
  243.                 ReDim .X(1 To .NumPoints)
  244.                 ReDim .Y(1 To .NumPoints)
  245.             End With
  246.         Next pline
  247.     Next tween
  248.     ' For each endpoint, create the tween endpoints.
  249.     For pline = 1 To Frames(key2).NumPolylines
  250.         With Frames(key2).Poly(pline)
  251.             For point = 1 To .NumPoints
  252.                 ' Pick slopes for the start & end.
  253.                 If key2 > 1 Then
  254.                     key1 = key2 - (key3 - key2)
  255.                 Else
  256.                     key1 = key2
  257.                 End If
  258.                 x1 = Frames(key1).Poly(pline).X(point)
  259.                 y1 = Frames(key1).Poly(pline).Y(point)
  260.                 x2 = .X(point)
  261.                 y2 = .Y(point)
  262.                 x3 = Frames(key3).Poly(pline).X(point)
  263.                 y3 = Frames(key3).Poly(pline).Y(point)
  264.                 If key3 < NumFrames Then
  265.                     key4 = key3 + (key3 - key2)
  266.                 Else
  267.                     key4 = key3
  268.                 End If
  269.                 x4 = Frames(key4).Poly(pline).X(point)
  270.                 y4 = Frames(key4).Poly(pline).Y(point)
  271.                 dx1 = x3 - x1
  272.                 dy1 = y3 - y1
  273.                 dx2 = x4 - x2
  274.                 dy2 = y4 - y2
  275.                 ' Compute the Hermite values.
  276.                 For tween = key2 + 1 To key3 - 1
  277.                     t = (tween - key2) / (key3 - key2)
  278.                     t2 = t * t
  279.                     t3 = t * t2
  280.                     A = 2 * t3 - 3 * t2 + 1
  281.                     B = -2 * t3 + 3 * t2
  282.                     C = t3 - 2 * t2 + t
  283.                     D = t3 - t2
  284.                     Frames(tween).Poly(pline).X(point) = x2 * A + x3 * B + dx1 * C + dx2 * D
  285.                     Frames(tween).Poly(pline).Y(point) = y2 * A + y3 * B + dy1 * C + dy2 * D
  286.                 Next tween
  287.             Next point
  288.         End With
  289.     Next pline
  290. End Sub
  291. ' ************************************************
  292. ' Insert a frame next to the selected one.
  293. ' ************************************************
  294. Private Sub AddFrame()
  295. Dim i As Integer
  296.     NumFrames = NumFrames + 1
  297.     ReDim Preserve Frames(1 To NumFrames)
  298.     For i = NumFrames - 1 To SelectedFrame Step -1
  299.         CopyFrame i, i + 1
  300.     Next i
  301.     SBar.Max = NumFrames
  302.     mnuFrameDelete.Enabled = (NumFrames > 1)
  303.     DataModified = True
  304. End Sub
  305. ' ************************************************
  306. ' Copy a polyline from frame1 to frame2.
  307. ' ************************************************
  308. Sub CopyFrame(frame1 As Integer, frame2 As Integer)
  309. Dim pline As Integer
  310. Dim point As Integer
  311.     Frames(frame2).NumPolylines = Frames(frame1).NumPolylines
  312.     If Frames(frame2).NumPolylines < 1 Then
  313.         Erase Frames(frame2).Poly
  314.     Else
  315.         ReDim Frames(frame2).Poly(1 To Frames(frame2).NumPolylines)
  316.     End If
  317.     For pline = 1 To Frames(frame2).NumPolylines
  318.         With Frames(frame2).Poly(pline)
  319.             .NumPoints = Frames(frame1).Poly(pline).NumPoints
  320.             If .NumPoints < 1 Then
  321.                 Erase .X
  322.                 Erase .Y
  323.             Else
  324.                 ReDim .X(1 To .NumPoints)
  325.                 ReDim .Y(1 To .NumPoints)
  326.             End If
  327.             For point = 1 To .NumPoints
  328.                 .X(point) = Frames(frame1).Poly(pline).X(point)
  329.                 .Y(point) = Frames(frame1).Poly(pline).Y(point)
  330.             Next point
  331.         End With
  332.     Next pline
  333. End Sub
  334. ' ************************************************
  335. ' Return true if the data has not been modified,
  336. ' or the user has saved the changes, or the user
  337. ' wants to lose the changes.
  338. ' ************************************************
  339. Function DataSafe() As Boolean
  340. Dim ans As Integer
  341.     Do While DataModified
  342.         ans = MsgBox("The data has been modified." & _
  343.             " Do you want to save the changes?", _
  344.             vbYesNoCancel)
  345.         If ans = vbCancel Then Exit Do
  346.         If ans = vbNo Then
  347.             DataSafe = True
  348.             Exit Function
  349.         End If
  350.             
  351.         ' Otherwise save the data.
  352.         If FileLoaded <> "" Then
  353.             mnuFileSave_Click
  354.         Else
  355.             mnuFileSaveAs_Click
  356.         End If
  357.     Loop
  358.     DataSafe = Not DataModified
  359. End Function
  360. ' ************************************************
  361. ' Draw the indicated frame.
  362. ' ************************************************
  363. Sub DrawFrame(frame As Integer)
  364. Dim pline As Integer
  365. Dim point As Integer
  366.     Canvas.Cls
  367.     For pline = 1 To Frames(frame).NumPolylines
  368.         With Frames(frame).Poly(pline)
  369.             If .NumPoints >= 2 Then
  370.                 Canvas.Line (.X(1), .Y(1))-(.X(2), .Y(2))
  371.                 For point = 3 To .NumPoints
  372.                     Canvas.Line -(.X(point), .Y(point))
  373.                 Next point
  374.             End If
  375.         End With
  376.     Next pline
  377. End Sub
  378. ' ************************************************
  379. ' Save the data.
  380. ' ************************************************
  381. Sub SaveData(fname As String)
  382. Dim fnum As Integer
  383. Dim frame As Integer
  384. Dim pline As Integer
  385. Dim point As Integer
  386.     On Error GoTo SaveDataError
  387.     ' Open the file.
  388.     fnum = FreeFile
  389.     Open fname For Output As fnum
  390.     ' Save the number of frames.
  391.     Write #fnum, NumFrames
  392.     ' Save each frame.
  393.     For frame = 1 To NumFrames
  394.         With Frames(frame)
  395.             ' Save the number of polylines.
  396.             Write #fnum, .NumPolylines
  397.                     
  398.             ' Save each polyline.
  399.             For pline = 1 To .NumPolylines
  400.                 With .Poly(pline)
  401.                     ' Save the number of points.
  402.                     Write #fnum, .NumPoints
  403.                     For point = 1 To .NumPoints
  404.                         Write #fnum, .X(point), .Y(point)
  405.                     Next point
  406.                 End With
  407.             Next pline
  408.         End With
  409.     Next frame
  410.     Close fnum
  411.     FileLoaded = fname
  412.     Caption = "TweenSmooth [" & fname & "]"
  413.     DataModified = False
  414.     Exit Sub
  415. SaveDataError:
  416.     Beep
  417.     MsgBox "Error saving file " & fname & "." & _
  418.         vbCrLf & Format$(Err.Number) & " : " & _
  419.         Err.Description
  420.     Exit Sub
  421. End Sub
  422. ' ************************************************
  423. ' Load polyline frames from the file.
  424. ' ************************************************
  425. Sub LoadData(fname As String)
  426. Dim fnum As Integer
  427. Dim frame As Integer
  428. Dim pline As Integer
  429. Dim point As Integer
  430.     On Error GoTo SaveDataError
  431.     ' Open the file.
  432.     fnum = FreeFile
  433.     Open fname For Input As fnum
  434.     ' Read the number of frames.
  435.     Input #fnum, NumFrames
  436.     ReDim Frames(1 To NumFrames)
  437.     SBar.Max = NumFrames
  438.     ' Read each frame.
  439.     For frame = 1 To NumFrames
  440.         With Frames(frame)
  441.             ' Read the number of polylines.
  442.             Input #fnum, .NumPolylines
  443.             ReDim .Poly(1 To .NumPolylines)
  444.                     
  445.             ' Read each polyline.
  446.             For pline = 1 To .NumPolylines
  447.                 With .Poly(pline)
  448.                     ' Read the number of points.
  449.                     Input #fnum, .NumPoints
  450.                     ReDim .X(1 To .NumPoints)
  451.                     ReDim .Y(1 To .NumPoints)
  452.                     For point = 1 To .NumPoints
  453.                         Input #fnum, .X(point), .Y(point)
  454.                     Next point
  455.                 End With
  456.             Next pline
  457.         End With
  458.     Next frame
  459.     Close fnum
  460.     SelectFrame 1
  461.     FileLoaded = fname
  462.     Caption = "TweenSmooth [" & fname & "]"
  463.     DataModified = False
  464.     Exit Sub
  465. SaveDataError:
  466.     Beep
  467.     MsgBox "Error loading file " & fname & "." & _
  468.         vbCrLf & Format$(Err.Number) & " : " & _
  469.         Err.Description
  470.     Exit Sub
  471. End Sub
  472. ' ************************************************
  473. ' Select and display the indicated frame.
  474. ' ************************************************
  475. Sub SelectFrame(num As Integer)
  476.     SelectedFrame = num
  477.     ' If we're drawing, stop drawing.
  478.     If Drawing Then
  479.         Canvas.DrawMode = vbCopyPen
  480.         Drawing = False
  481.     End If
  482.     DrawFrame SelectedFrame
  483.     FrameLabel.Caption = Format$(SelectedFrame) _
  484.          & "/" & Format$(NumFrames)
  485.     SelectingFrame = True
  486.     SBar.Value = SelectedFrame
  487.     SelectingFrame = False
  488. End Sub
  489. ' ***********************************************
  490. ' Give the form and all the picture boxes an
  491. ' hourglass cursor.
  492. ' ***********************************************
  493. Sub WaitStart()
  494.     MousePointer = vbHourglass
  495.     Canvas.MousePointer = vbHourglass
  496.     DoEvents
  497. End Sub
  498. ' ***********************************************
  499. ' Restore the mouse pointers for the form and all
  500. ' the picture boxes.
  501. ' ***********************************************
  502. Sub WaitEnd()
  503.     MousePointer = vbDefault
  504.     Canvas.MousePointer = vbDefault
  505. End Sub
  506. Private Sub Canvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  507.     If Drawing And Button = vbRightButton Then
  508.         ' End the previous polyline.
  509.         Canvas.Line (StartX, StartY)-(LastX, LastY)
  510.         Canvas.DrawMode = vbCopyPen
  511.         Drawing = False
  512.         Exit Sub
  513.     End If
  514.     ' See if this is the start of a new polyline.
  515.     If Drawing Then
  516.         ' Nope. Erase the previous line.
  517.         Canvas.Line (StartX, StartY)-(LastX, LastY)
  518.     Else
  519.         ' Start a new polyline.
  520.         With Frames(SelectedFrame)
  521.             .NumPolylines = .NumPolylines + 1
  522.             ReDim Preserve .Poly(1 To .NumPolylines)
  523.             With .Poly(.NumPolylines)
  524.                 .NumPoints = 1
  525.                 ReDim .X(1 To 1)
  526.                 ReDim .Y(1 To 1)
  527.                 .X(1) = X
  528.                 .Y(1) = Y
  529.             End With
  530.         End With
  531.         Canvas.DrawMode = vbInvert
  532.         Drawing = True
  533.         DataModified = True
  534.         StartX = X
  535.         StartY = Y
  536.     End If
  537.     LastX = X
  538.     LastY = Y
  539.     Canvas.Line (StartX, StartY)-(LastX, LastY)
  540. End Sub
  541. Private Sub Canvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  542.     If Not Drawing Then Exit Sub
  543.     Canvas.Line (StartX, StartY)-(LastX, LastY)
  544.     LastX = X
  545.     LastY = Y
  546.     Canvas.Line (StartX, StartY)-(LastX, LastY)
  547. End Sub
  548. Private Sub Canvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  549.     If Not Drawing Then Exit Sub
  550.     Canvas.Line (StartX, StartY)-(LastX, LastY)
  551.     Canvas.DrawMode = vbCopyPen
  552.     Canvas.Line (StartX, StartY)-(X, Y)
  553.     Canvas.DrawMode = vbInvert
  554.     With Frames(SelectedFrame)
  555.         With .Poly(.NumPolylines)
  556.             .NumPoints = .NumPoints + 1
  557.             ReDim Preserve .X(1 To .NumPoints)
  558.             ReDim Preserve .Y(1 To .NumPoints)
  559.             .X(.NumPoints) = X
  560.             .Y(.NumPoints) = Y
  561.         End With
  562.     End With
  563.     DataModified = True
  564.     StartX = X
  565.     StartY = Y
  566. End Sub
  567. ' ************************************************
  568. ' Play the animation.
  569. ' ************************************************
  570. Private Sub CmdPlay_Click()
  571.     If Playing Then
  572.         Playing = False
  573.         CmdPlay.Caption = "Stopped"
  574.         CmdPlay.Enabled = False
  575.     Else
  576.         Playing = True
  577.         CmdPlay.Caption = "Stop"
  578.         PlayData
  579.         CmdPlay.Caption = "Play"
  580.         Playing = False
  581.         CmdPlay.Enabled = True
  582.         DrawFrame SelectedFrame
  583.     End If
  584. End Sub
  585. ' ************************************************
  586. ' Play the animation.
  587. ' ************************************************
  588. Sub PlayData()
  589. Dim mpf As Long     ' Milliseconds per frame.
  590. Dim frame As Integer
  591. Dim next_time As Long
  592. Dim play_type As Integer
  593. Dim num As Integer
  594. Dim start_time As Single
  595. Dim stop_time As Single
  596.     ' See how fast we should go.
  597.     If Not IsNumeric(FPSText.Text) Then _
  598.         FPSText.Text = "10"
  599.     mpf = 1000 \ CLng(FPSText.Text)
  600.     ' See what kind of animation this should be.
  601.     For play_type = 0 To 2
  602.         If PlayOption(play_type).Value Then Exit For
  603.     Next play_type
  604.     If play_type > 2 Then play_type = 0
  605.     ' Start the animation.
  606.     start_time = Timer
  607.     next_time = GetTickCount()
  608.     Do While Playing
  609.         ' Show the frames.
  610.         For frame = 1 To NumFrames
  611.             If Not Playing Then Exit Do
  612.             num = num + 1
  613.             
  614.             ' Draw the frame.
  615.             DrawFrame frame
  616.                 
  617.             ' Wait until it's time for the next frame.
  618.             next_time = next_time + mpf
  619.             WaitTill next_time
  620.         Next frame
  621.         ' If this is a one time deal, stop now.
  622.         If play_type = 0 Then Exit Do
  623.         
  624.         ' If this is a reversing run, go backwards.
  625.         If play_type = 2 Then
  626.             For frame = NumFrames - 1 To 2 Step -1
  627.                 If Not Playing Then Exit Do
  628.                 num = num + 1
  629.                 
  630.                 ' Draw the frame.
  631.                 DrawFrame frame
  632.                     
  633.                 ' Wait until it's time for the next frame.
  634.                 next_time = next_time + mpf
  635.                 WaitTill next_time
  636.             Next frame
  637.         End If
  638.     Loop
  639.     stop_time = Timer
  640.     MsgBox "Displayed" & Str$(num) & _
  641.         " frames in " & _
  642.         Format$(stop_time - start_time, "0.00") & _
  643.         " seconds (" & _
  644.         Format$(num / (stop_time - start_time), "0.00") & _
  645.         " FPS)."
  646. End Sub
  647. ' ************************************************
  648. ' Make the tweens.
  649. ' ************************************************
  650. Private Sub CmdTween_Click()
  651. Dim num_tweens As Integer
  652. Dim old_frames As Integer
  653. Dim frame1 As Integer
  654. Dim frame2 As Integer
  655. Dim frame As Integer
  656.     ' See how many tweens to make.
  657.     If Not IsNumeric(TweensText.Text) Then _
  658.         TweensText.Text = "4"
  659.     num_tweens = TweensText.Text
  660.     If num_tweens < 1 Then num_tweens = 1
  661.     ' Make room for the new frames.
  662.     old_frames = NumFrames
  663.     NumFrames = num_tweens * (NumFrames - 1) + NumFrames
  664.     ReDim Preserve Frames(1 To NumFrames)
  665.     ' Spread the original frames out.
  666.     For frame = old_frames To 2 Step -1
  667.         CopyFrame frame, _
  668.             num_tweens * (frame - 1) + frame
  669.     Next frame
  670.     ' Make the tweens.
  671.     For frame = 1 To old_frames - 1
  672.         frame1 = num_tweens * (frame - 1) + frame
  673.         frame2 = frame1 + num_tweens + 1
  674.         MakeTweens frame1, frame2
  675.     Next frame
  676.     SBar.Max = NumFrames
  677.     SelectFrame num_tweens * (SelectedFrame - 1) + _
  678.         SelectedFrame
  679.     DataModified = True
  680. End Sub
  681. Private Sub Form_Load()
  682.     ' Position the scroll bar.
  683.     SBar.Top = Canvas.Top + Canvas.Height + 1
  684.     ' Create an empty frame.
  685.     mnuFileNew_Click
  686. End Sub
  687. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  688.     Cancel = Not DataSafe()
  689. End Sub
  690. Private Sub Form_Unload(Cancel As Integer)
  691.     End
  692. End Sub
  693. Private Sub mnuFileExit_Click()
  694.     Unload Me
  695. End Sub
  696. ' ************************************************
  697. ' Load a data file.
  698. ' ************************************************
  699. Private Sub mnuFileLoad_Click()
  700. Dim fname As String
  701.     If Not DataSafe() Then Exit Sub
  702.     ' Allow the user to pick a file.
  703.     On Error Resume Next
  704.     FileDialog.FilterIndex = 1
  705.     FileDialog.filename = "*.TWE"
  706.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  707.     FileDialog.ShowOpen
  708.     If Err.Number = cdlCancel Then
  709.         Exit Sub
  710.     ElseIf Err.Number <> 0 Then
  711.         Beep
  712.         MsgBox "Error selecting file.", , vbExclamation
  713.         Exit Sub
  714.     End If
  715.     On Error GoTo 0
  716.     fname = Trim$(FileDialog.filename)
  717.     FileDialog.InitDir = Left$(fname, Len(fname) _
  718.         - Len(FileDialog.FileTitle) - 1)
  719.     ' Load the data file.
  720.     WaitStart
  721.     LoadData fname
  722.     WaitEnd
  723.     FrameLabel.Caption = Format$(SelectedFrame) _
  724.          & "/" & Format$(NumFrames)
  725. End Sub
  726. ' ************************************************
  727. ' Clear out all the data.
  728. ' ************************************************
  729. Private Sub mnuFileNew_Click()
  730.     If Not DataSafe() Then Exit Sub
  731.     NumFrames = 1
  732.     ReDim Frames(1 To NumFrames)
  733.     Frames(1).NumPolylines = 0
  734.     SBar.Max = NumFrames
  735.     SelectFrame 1
  736. End Sub
  737. ' ************************************************
  738. ' Save the data file.
  739. ' ************************************************
  740. Private Sub mnuFileSave_Click()
  741.     If FileLoaded = "" Then
  742.         mnuFileSaveAs_Click
  743.         Exit Sub
  744.     End If
  745.     WaitStart
  746.     SaveData FileLoaded
  747.     WaitEnd
  748. End Sub
  749. ' ************************************************
  750. ' Save the data file with a new name.
  751. ' ************************************************
  752. Private Sub mnuFileSaveAs_Click()
  753. Dim fname As String
  754.     ' Allow the user to pick a file.
  755.     On Error Resume Next
  756.     FileDialog.FilterIndex = 1
  757.     FileDialog.filename = "*.TWE"
  758.     FileDialog.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  759.     FileDialog.ShowSave
  760.     If Err.Number = cdlCancel Then
  761.         Exit Sub
  762.     ElseIf Err.Number <> 0 Then
  763.         Beep
  764.         MsgBox "Error selecting file.", , vbExclamation
  765.         Exit Sub
  766.     End If
  767.     On Error GoTo 0
  768.     fname = Trim$(FileDialog.filename)
  769.     FileDialog.InitDir = Left$(fname, Len(fname) _
  770.         - Len(FileDialog.FileTitle) - 1)
  771.     ' Save the script file.
  772.     WaitStart
  773.     SaveData fname
  774.     WaitEnd
  775. End Sub
  776. ' ************************************************
  777. ' Insert a frame after the selected one.
  778. ' ************************************************
  779. Private Sub mnuFrameAfter_Click()
  780.     AddFrame
  781.     SelectFrame SelectedFrame + 1
  782. End Sub
  783. ' ************************************************
  784. ' Insert a frame before the selected one.
  785. ' ************************************************
  786. Private Sub mnuFrameBefore_Click()
  787.     AddFrame
  788.     FrameLabel.Caption = Format$(SelectedFrame) & "/" & Format$(NumFrames)
  789. End Sub
  790. ' ************************************************
  791. ' Remove the polylines from the selected frame.
  792. ' ************************************************
  793. Private Sub mnuFrameClear_Click()
  794. Dim i As Integer
  795.     With Frames(SelectedFrame)
  796.         .NumPolylines = 0
  797.         Erase .Poly
  798.     End With
  799.     SelectFrame SelectedFrame
  800.     DataModified = True
  801. End Sub
  802. ' ************************************************
  803. ' Delete the selected frame.
  804. ' ************************************************
  805. Private Sub mnuFrameDelete_Click()
  806. Dim i As Integer
  807.     For i = SelectedFrame To NumFrames - 1
  808.         CopyFrame i + 1, i
  809.     Next i
  810.     NumFrames = NumFrames - 1
  811.     ReDim Preserve Frames(1 To NumFrames)
  812.     SBar.Max = NumFrames
  813.     If SelectedFrame > NumFrames Then _
  814.        SelectedFrame = NumFrames
  815.     SelectFrame SelectedFrame
  816.     mnuFrameDelete.Enabled = (NumFrames > 1)
  817.     DataModified = True
  818. End Sub
  819. ' ************************************************
  820. ' Select a new frame.
  821. ' ************************************************
  822. Private Sub SBar_Change()
  823.     If SelectingFrame Then Exit Sub
  824.     SelectFrame SBar.Value
  825. End Sub
  826. ' ************************************************
  827. ' Select a new frame.
  828. ' ************************************************
  829. Private Sub SBar_Scroll()
  830.     SBar_Change
  831. End Sub
  832.